//
//  CreatChatRoomViewController.m
//  XMPPHelper
//
//  Created by 蔡成汉 on 15/5/14.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "CreatChatRoomViewController.h"
#import "XMPPHelper.h"

@interface CreatChatRoomViewController ()<UITextFieldDelegate>
{
    UITextField *userNameTextField;
}
@end

@implementation CreatChatRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"创建聊天室";
    
    if ([[UIDevice currentDevice] systemVersion].floatValue>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 100, 200, 30)];
    userNameTextField.delegate = self;
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    userNameTextField.placeholder = @"用户名";
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTextField.returnKeyType = UIReturnKeyNext;
    [userNameTextField becomeFirstResponder];
    userNameTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.view addSubview:userNameTextField];
    
    UIButton *creatChatRoomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    creatChatRoomButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 150, 200, 30);
    [creatChatRoomButton setTitle:@"创建聊天室" forState:UIControlStateNormal];
    [creatChatRoomButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal
     ];
    [creatChatRoomButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [creatChatRoomButton addTarget:self action:@selector(creatChatRoomButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:creatChatRoomButton];
    
    UIButton *joinChatRoomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    joinChatRoomButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 200, 200, 30);
    [joinChatRoomButton setTitle:@"加入聊天室" forState:UIControlStateNormal];
    [joinChatRoomButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal
     ];
    [joinChatRoomButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [joinChatRoomButton addTarget:self action:@selector(joinChatRoomButtonButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:joinChatRoomButton];
    
}

/**
 *  创建聊天室
 *
 *  @param paramSender 创建聊天室按钮
 */
-(void)creatChatRoomButtonIsTouch:(UIButton *)paramSender
{
    XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
    [myXMPPHelper creatChatRoom:userNameTextField.text];
}

/**
 *  加入聊天室
 *
 *  @param paramSender 加入聊天室按钮
 */
-(void)joinChatRoomButtonButtonIsTouch:(UIButton *)paramSender
{
    XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
    [myXMPPHelper joinChatRoom:userNameTextField.text];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
